/**
 *  @Author MJreams
 *  @Time 2021/12/6 12:25:00
 *
**/

package router
import (
	"Educational/config"
	"Educational/controller"
	"Educational/pkg/log"
	"fmt"
	"github.com/gin-contrib/cors"
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
)


type Router struct {
	router *gin.Engine
	cfg config.Config
}

func New(cfg config.Config,db *gorm.DB) Router {
	router := gin.Default()
	router.Use(gin.Logger())
	router.Use(cors.Default())

	ctl := controller.New(cfg,db)

	api := router.Group("/api")
	api.POST("/login", ctl.Login)
	//api.POST("/test", ctl.Test)
	api.POST("/schedule", ctl.Schedule)

	return Router{
		router: router,
		cfg:    cfg,
	}
}

func (r *Router) Run() error {
	log.Logger.Info(fmt.Sprintf("%s:%s", r.cfg.Server.Host, r.cfg.Server.Port))
	return r.router.Run(fmt.Sprintf("%s:%s", r.cfg.Server.Host, r.cfg.Server.Port))
}
